package com.hyz.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hyz.reggie.common.BaseContext;
import com.hyz.reggie.pojo.Employee;
import com.hyz.reggie.pojo.R;
import com.hyz.reggie.service.EmployeeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
@RestController
@RequestMapping("/employee")
@Slf4j
public class EmployeeController {
    @Autowired
    private EmployeeService service;
    @Autowired
    private HttpServletRequest request;

    @PostMapping("/login")
    public R login(@RequestBody Employee employee) {
        if (employee == null) {
            return R.error("服务器繁忙请稍后重试~");
        }
        String username = employee.getUsername();
        String password = employee.getPassword();
        if (username == null || username.equals("") || password == null || password.equals("")) {
            return R.error("这边建议重新投胎做个人再来操作~");
        }
        password = DigestUtils.md5DigestAsHex(password.getBytes());
        LambdaQueryWrapper<Employee> qwq = new LambdaQueryWrapper<>();
        qwq.eq(Employee::getUsername, username);
        Employee one = service.getOne(qwq);
        if (one == null) {
            return R.error("用户不存在，请确认后重新输入~");
        }
        if (!password.equals(one.getPassword())) {
            return R.error("密码错误~");
        }
        if(one.getStatus()==0){
            return R.error("你的账号已被禁用，请联系管理员处理~");
        }
        Long loginId = one.getId();
        HttpSession session = request.getSession();
        session.setAttribute("employee", loginId);
        return R.success(one);
    }

    @PostMapping("/logout")
    public R loginOut() {
        HttpSession session = request.getSession();
        session.setAttribute("employee", null);
        return R.success("退出成功~");
    }

    @PostMapping
    public R addUser(@RequestBody Employee employee) {
        LambdaQueryWrapper<Employee> qwq = new LambdaQueryWrapper<>();
        qwq.eq(Employee::getUsername, employee.getUsername());
        Employee one = service.getOne(qwq);
        if (one != null) {
            return R.error("用户名已存在，请重新输入~");
        }
        String passWord = DigestUtils.md5DigestAsHex("112233".getBytes());
        employee.setPassword(passWord);
        service.save(employee);
        return R.success("新增成功，欢迎你的加入~");
    }

    @GetMapping("/page")
    public R findAll(Integer page, Integer pageSize, String name) {
        if (page == null || page.equals("") || pageSize == null || pageSize.equals("") || pageSize <= 0) {
            return R.error("看，是傻逼~");
        }
        IPage<Employee> iPage = new Page<>(page, pageSize);
        LambdaQueryWrapper<Employee> qwq = new LambdaQueryWrapper();
        if (name != null) {
            qwq.like(Employee::getName, name);
        }
        iPage = service.page(iPage, qwq);
        return R.success(iPage);
    }

    @PutMapping
    public R modifyStatus(@RequestBody Employee employee) {
        if (employee == null) {
            return R.error("做个人吧你~");

        }
        if (employee.getId() == 1) {
            return R.error("无法修改管理员账户~");
        }
        Long enpId = BaseContext.getCurrentID();
        if (enpId.equals(employee.getId()) || enpId.equals(1L)) {
            service.updateById(employee);
            return R.success("修改成功~");
        }
        return R.error("权限不足~");
    }

    @GetMapping("/{id}")
    public R selectById(@PathVariable Long id) {
        if (id == null) {
            return R.error("做个人吧你~");
        }
        Employee employee = service.getById(id);
        return R.success(employee);
    }
}
